// 1.将data数据通过_proxyData挂载到vue的实例上
// 2.通过Observer将data设置setter、getter
// 3.Compiler类根据元素的所有节点 渲染指令 和 文本渲染
// 4.Dep 类收集修改后需要更新的方法
// 5.Watcher类观察者
class LxVue {
    constructor(options) {
        this.$options = options
        this.$data = options.data || {}
        this.$el = typeof options.el === "string" ? document.querySelector(options.el) : options.el

        this._proxyData(this.$data)

        new Observer(this.$data)
        new Compiler(this)
    }

    _proxyData(data) {
        for (let key in data) {
            Object.defineProperty(this, key, {
                enumerable: true,
                configurable: true,
                get() {
                    return data[key]
                },
                set(val) {
                    data[key] = val
                },
            })
        }
    }
}

// 将$data数据变成响应式
class Observer {
    constructor(data) {
        this.walk(data)
    }
    // 遍历对象的所有属性
    walk(data) {
        if (!data || typeof data !== "object") return
        Object.keys(data).forEach(key => {
            this.defineReactive(data, key, data[key])
        })
    }

    defineReactive(obj, key, val) {
        this.walk(val)
        let dep = new Dep()
        let self = this
        Object.defineProperty(obj, key, {
            enumerable: true,
            configurable: true,
            get() {
                Dep.target && dep.addSub(Dep.target)
                return val
            },
            set(newValue) {
                if (newValue === val) {
                    return
                }
                val = newValue
                self.walk(val)
                // 发送通知
                dep.notify()
            }
        })
    }
}

class Compiler {
    constructor(vm) {
        this.el = vm.$el
        this.vm = vm
        this.compile(this.el)
    }

    compile(el) {
        let childNodes = el.childNodes;
        Array.from(childNodes).forEach(node => {
            if (this.isTextNode(node)) {
                this.compileText(node)
            } else if (this.isElementNode) {
                this.compileElement(node)
            }

            if (node.childNodes && node.childNodes.length) {
                this.compile(node)
            }
        })
    }

    compileElement(node) {
        Array.from(node.attributes).forEach(attr => {
            let attrName = attr.name
            if (this.isDirective(attr.name)) {
                attrName = attrName.substr(2)
                let key = attr.value
                this[`${attrName}Updater`] && this[`${attrName}Updater`](node, this.vm[key], key)
            }
        })
    }

    textUpdater(node, value, key) {
        node.textContent = value
        new Wacther(this.vm, key, newValue => node.textContent = newValue)
    }
    modelUpdater(node, value, key) {
        node.value = value
        new Wacther(this.vm, key, newValue => node.value = newValue)

        node.addEventListener("input", (e) => {
            this.vm[key] = e.target.value
        })
    }

    compileText(node) {
        let reg = /\{\{(.+?)\}\}/
        let value = node.textContent
        if (reg.test(value)) {
            let key = RegExp.$1.trim()
            let oldValue = value
            node.textContent = value.replace(reg, this.vm[key])
            new Wacther(this.vm, key, newValue => node.textContent = oldValue.replace(reg, newValue))
        }
    }

    isDirective(attrName) {
        return attrName.startsWith("v-")
    }

    isTextNode(node) {
        return node.nodeType === 3
    }

    isElementNode(node) {
        return node.nodeType === 1
    }

}

class Dep {
    constructor() {
        this.subs = []
    }

    addSub(sub) {
        sub && sub.update && this.subs.push(sub)
    }

    notify() {
        this.subs.forEach(sub => {
            sub.update()
        })
    }

}

class Wacther {
    constructor(vm, key, cb) {
        this.vm = vm;
        this.key = key;
        this.cb = cb;
        Dep.target = this
        this.oldValue = vm[key]
        Dep.target = null
    }
    update() {
        let newValue = this.vm[this.key]
        this.cb(newValue)
    }
}